import dayjs from "dayjs";

// 获取应用实例
const app = getApp();

// 初始化数据库
const db = wx.cloud.database();
const uimg = db.collection("uimg");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    theme: 1,
    content: "",
    time: "",
    openid: "",
    o_openid: '',
    alreadySet: false,
    memorList: [],
    showTime: false,
    currentDate: +new Date(),
    maxDate: new Date().getTime(),
    minDate: new Date(1949, 1, 1).getTime(),
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      return value;
    },

    navHeight: app.globalData.navBarHeight,
    menuHeight: app.globalData.menuHeight,
    menuRight: app.globalData.menuRight,
    menuTop: app.globalData.menuTop,
  },

  onInput(event) {
    let { time } = this.data;
    time = dayjs(event.detail).format("YYYY/MM/DD");
    this.setData({
      time,
    });
  },

  onCancel() {
    this.setData({
      showTime: false,
      time: "",
    });
  },

  onFinish() {
    this.setData({
      showTime: false,
    });
  },

  onFocus() {
    this.setData({
      showTime: true,
    });
  },

  onChange(e) {
    let { content } = this.data;
    content = e.detail;
    this.setData({
      content,
    });
  },

  // 获取时间
  getLoveTime(openid) {
    const that = this;
    wx.showLoading({
      title: "Loading...",
      mask: true,
    });
    uimg.where({ _openid: openid }).get({
      success: (res) => {
        if (res.data[0].love_time) {
          that.setData({
            time: res.data[0].love_time,
            alreadySet: true,
            currentDate: new Date(res.data[0].love_time).getTime(),
          });
        } else {
          that.setData({
            alreadySet: false,
          });
        }
      },
      fail: (err) => {
        wx.showModal({
          title: "错误",
          content: err,
          showCancel: true,
          cancelText: "取消",
          cancelColor: "#000000",
          confirmText: "确定",
          confirmColor: "#a5646d",
        });
      },
      complete: () => {
        wx.hideLoading();
      },
    });
  },

  // 设置时间
  setLoveTime() {
    let { time, openid, o_openid, alreadySet } = this.data;
    if (openid == "") {
      wx.showModal({
        title: "提示",
        content: "openid 失效，请重新进入小程序",
      });
      return;
    }
    if (time == "") {
      wx.showToast({
        title: '请填写相恋时间哦~',
        icon: "none"
      })
      return;
    }
    uimg.where({ _openid: openid }).update({
      data: {
        love_time: time,
      },
      success: function (res) {
        uimg.where({ _openid: o_openid }).update({
          data: {
            love_time: time,
          },
          success: function (res) {
            app.globalData.loveTime = time;

            if (alreadySet) {
              wx.showToast({
                title: "更新成功",
                icon: "success",
              });
            } else {
              wx.showToast({
                title: "设置成功",
                icon: "success",
              });
            }

            wx.navigateBack(1);
          },
        });
      },
    });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const openid = wx.getStorageSync("openid");
    const o_openid = wx.getStorageSync("o_openid");
    this.getLoveTime(openid);

    this.setData({
      theme: app.globalData.theme,
      openid: openid || "",
      o_openid: o_openid || ''
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
